package com.free.semantic.infrastruct.repository.impl;

import com.free.semantic.infrastruct.common.ConceptObjectDO;
import com.free.semantic.infrastruct.mapper.ConceptObjectMapper;
import com.free.semantic.infrastruct.repository.ConceptObjectRepository;
import com.free.semantic.model.conceptmodel.ConceptObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class ConceptObjectRepositoryImpl implements ConceptObjectRepository {

    @Autowired
    private ConceptObjectMapper conceptObjectMapper;

    @Override
    public ConceptObject findById(Long id) {
        ConceptObjectDO conceptObjectDO = conceptObjectMapper.findById(id);
        if (conceptObjectDO == null) {
            return null;
        }
        return convertToModel(conceptObjectDO);
    }

    @Override
    public List<ConceptObject> findBySemanticNetworkId(Long semanticNetworkId) {
        List<ConceptObjectDO> conceptObjectDOs = conceptObjectMapper.findBySemanticNetworkId(semanticNetworkId);
        return conceptObjectDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public Long save(ConceptObject conceptObject) {
        ConceptObjectDO conceptObjectDO = convertToDO(conceptObject);
        conceptObjectMapper.insert(conceptObjectDO);
        return conceptObjectDO.getId();
    }

    @Override
    public void update(ConceptObject conceptObject) {
        ConceptObjectDO conceptObjectDO = convertToDO(conceptObject);
        conceptObjectMapper.update(conceptObjectDO);
    }

    @Override
    public void deleteById(Long id) {
        conceptObjectMapper.deleteById(id);
    }

    @Override
    public void deleteBySemanticNetworkId(Long semanticNetworkId) {
        conceptObjectMapper.deleteBySemanticNetworkId(semanticNetworkId);
    }

    private ConceptObject convertToModel(ConceptObjectDO conceptObjectDO) {
        ConceptObject conceptObject = new ConceptObject();
        conceptObject.setId(conceptObjectDO.getId());
        conceptObject.setName(conceptObjectDO.getName());
        conceptObject.setSemanticNetworkId(conceptObjectDO.getSemanticNetworkId());
        return conceptObject;
    }

    private ConceptObjectDO convertToDO(ConceptObject conceptObject) {
        ConceptObjectDO conceptObjectDO = new ConceptObjectDO();
        conceptObjectDO.setId(conceptObject.getId());
        conceptObjectDO.setName(conceptObject.getName());
        conceptObjectDO.setSemanticNetworkId(conceptObject.getSemanticNetworkId());
        return conceptObjectDO;
    }
} 